﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using System.Windows.Media;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using MSPToolkit.Models;
using MSPToolkit.Utilities;
using System.Windows;

namespace MSPToolkitSampleApp.UtilitesSamples
{
    public partial class TileGeneratorSample : PhoneApplicationPage
    {
        public TileGeneratorSample()
        {
            InitializeComponent();
        }

        private void CreateMosaicTile(object sender, MouseButtonEventArgs e)
        {
            var uri1 = new Uri("Images/1.png", UriKind.Relative);
            var uri2 = new Uri("Images/2.png", UriKind.Relative);
            var uri3 = new Uri("Images/3.png", UriKind.Relative);
            var uri4 = new Uri("Images/Background.png", UriKind.Relative);
            var uri5 = new Uri("Images/5.png", UriKind.Relative);
            var uri6 = new Uri("Images/6.png", UriKind.Relative);
            var uri7 = new Uri("Images/7.png", UriKind.Relative);
            var uri8 = new Uri("Images/8.png", UriKind.Relative);
            var uri9 = new Uri("Images/9.png", UriKind.Relative);
            var uri10 = new Uri("Images/6.png", UriKind.Relative);
            var uri11 = new Uri("Images/4.png", UriKind.Relative);
            var uri12 = new Uri("Images/5.png", UriKind.Relative);
            var uri13 = new Uri("Images/7.png", UriKind.Relative);
            var uri14 = new Uri("Images/3.png", UriKind.Relative);
            var uri15 = new Uri("Images/Background.png", UriKind.Relative);
            var uri16 = new Uri("Images/2.png", UriKind.Relative);
            var uri17 = new Uri("Images/1.png", UriKind.Relative);
            var uri18 = new Uri("Images/8.png", UriKind.Relative);
            var tile = ShellTile.ActiveTiles.FirstOrDefault(t => t.NavigationUri == new Uri("/UtilitesSamples/TileGeneratorSample.xaml", UriKind.Relative));
            if (tile != null)
                tile.Delete();
            if (!Mangopollo.Utils.CanUseLiveTiles)
            {
                var tileUrl = TileImageGenerator.GenerateTile(uri1, uri2, uri3, uri4, uri5, uri6, uri7, uri8, uri9, "MosaicLiveTileImage");
                ShellTile.Create(new Uri("/UtilitesSamples/TileGeneratorSample.xaml", UriKind.Relative), new StandardTileData { Title = "Mosaic", BackgroundImage = tileUrl });
            }
            else
            {
                var tileUrl = TileImageGenerator.GenerateTile(uri1, uri2, uri3, uri4, uri5, uri6, uri7, uri8, uri9, "MosaicLiveTileImage", TileSize.Wp8TileSize);
                var smallUrl = TileImageGenerator.GenerateTile(uri1, uri2, uri3, uri4, uri5, uri6, uri7, uri8, uri9, "MosaicLiveTileSmallImage", TileSize.Wp8SmallTileSize);
                var wideUrl = TileImageGenerator.GenerateTile(uri1, uri2, uri3, uri4, uri5, uri6, uri7, uri8, uri9,
                                                              uri10, uri11, uri12, uri13, uri14, uri15, uri16, uri17, uri18,
                                                              "MosaicLiveTileWideImage");
                var tileData = Mangopollo.Tiles.TilesCreator.CreateFlipTile("Graph", null, null, null, null, smallUrl, tileUrl, null, wideUrl, null);
                Mangopollo.Tiles.ShellTileExt.Create(new Uri("/UtilitesSamples/TileGeneratorSample.xaml", UriKind.Relative), tileData, true);
            }
        }

        private void CreateTileFromUris(object sender, MouseButtonEventArgs e)
        {
            var uris = new List<UriWithTransform>
            {
                new UriWithTransform{ Uri =new Uri("Images/1.png", UriKind.Relative), Height=30, Width=30, TranslateTransform=new TranslateTransform() {X=12, Y=30 }},
                new UriWithTransform{ Uri =new Uri("Images/5.png", UriKind.Relative), Height=30, Width=30, TranslateTransform=new TranslateTransform() { X=12,Y=80}}
            };

            var tileUrl = TileImageGenerator.GenerateTile(uris, "UrisLiveTileImage");
            var tile = ShellTile.ActiveTiles.FirstOrDefault(t => t.NavigationUri == new Uri("/UtilitesSamples/TileGeneratorSample.xaml", UriKind.Relative));
            if (tile != null)
                tile.Delete();
            ShellTile.Create(new Uri("/UtilitesSamples/TileGeneratorSample.xaml", UriKind.Relative), new StandardTileData { Title = "Images", BackgroundImage = tileUrl });
        }

        private void CreateTileFromStrings(object sender, MouseButtonEventArgs e)
        {
            var strings = new List<StringsWithTransform>
            {
                new StringsWithTransform{ Text="color1", TranslateTransform=new TranslateTransform() { X=48, Y=30 }},
                new StringsWithTransform{ Text="color2", TranslateTransform=new TranslateTransform() { X=48, Y=80 }}
            };

            var tileUrl = TileImageGenerator.GenerateTile(strings, "UrisLiveTileImage");

            var tile = ShellTile.ActiveTiles.FirstOrDefault(t => t.NavigationUri == new Uri("/UtilitesSamples/TileGeneratorSample.xaml", UriKind.Relative));
            if (tile != null)
                tile.Delete();
            ShellTile.Create(new Uri("/UtilitesSamples/TileGeneratorSample.xaml", UriKind.Relative), new StandardTileData { Title = "Text", BackgroundImage = tileUrl });
        }

        private void CreateTileFromStringsAndUris(object sender, MouseButtonEventArgs e)
        {
            var strings = new List<StringsWithTransform>
            {
                new StringsWithTransform{ Text="color1", TranslateTransform=new TranslateTransform() { X=48, Y=30 }},
                new StringsWithTransform{ Text="color2", TranslateTransform=new TranslateTransform() { X=48, Y=80 }}
            };

            var uris = new List<UriWithTransform>
            {
                new UriWithTransform{ Uri =new Uri("Images/1.png", UriKind.Relative), Height=30, Width=30, TranslateTransform=new TranslateTransform() {X=12, Y=30 }},
                new UriWithTransform{ Uri =new Uri("Images/5.png", UriKind.Relative), Height=30, Width=30, TranslateTransform=new TranslateTransform() { X=12,Y=80}}
            };

            var tileUrl = TileImageGenerator.GenerateTile(strings.Select(ImageGenerator.StringToTextBlockWithTransform), uris.Select(ImageGenerator.UriToImageWithTransform), "UrisLiveTileImage");

            var tile = ShellTile.ActiveTiles.FirstOrDefault(t => t.NavigationUri == new Uri("/UtilitesSamples/TileGeneratorSample.xaml", UriKind.Relative));
            if (tile != null)
                tile.Delete();
            ShellTile.Create(new Uri("/UtilitesSamples/TileGeneratorSample.xaml", UriKind.Relative), new StandardTileData { Title = "Text and images", BackgroundImage = tileUrl });
        }

        private void CreateTileAsMessageTile(object sender, MouseButtonEventArgs e)
        {
            var tile = ShellTile.ActiveTiles.FirstOrDefault(t => t.NavigationUri == new Uri("/UtilitesSamples/TileGeneratorSample.xaml", UriKind.Relative));
            if (tile != null)
                tile.Delete();            
            if (!Mangopollo.Utils.CanUseLiveTiles)
            {
                var tileUrl = TileImageGenerator.GenerateTile(new Uri("Images/StarTileBackground.png", UriKind.Relative), 7, "UrisLiveTileImage");
                ShellTile.Create(new Uri("/UtilitesSamples/TileGeneratorSample.xaml", UriKind.Relative), new StandardTileData { Title = "Image + number", BackgroundImage = tileUrl });
            }
            else
            {
                var tileUrl = TileImageGenerator.GenerateTile(new Uri("Images/StarTileBackground.png", UriKind.Relative), 8, "UrisLiveTileImage", TileSize.Wp8TileSize);
                var smallUrl = TileImageGenerator.GenerateTile(new Uri("Images/StarTileBackground.png", UriKind.Relative), 8, "UrisLiveTileSmallImage", TileSize.Wp8SmallTileSize);
                var wideUrl = TileImageGenerator.GenerateTile(new Uri("Images/StarTileBackground.png", UriKind.Relative), 8, "UrisLiveTileWideImage", TileSize.Wp8WideTileSize);
                var tileData = Mangopollo.Tiles.TilesCreator.CreateFlipTile("Image + number", null, "Yes, flip Iconic tile, if you need", null, null, smallUrl, tileUrl, null, wideUrl, null);
                Mangopollo.Tiles.ShellTileExt.Create(new Uri("/UtilitesSamples/TileGeneratorSample.xaml", UriKind.Relative), tileData, true);
            }
        }

        private void CreateMultilayerTile(object sender, MouseButtonEventArgs e)
        {
            var uris = new List<UriWithTransform>
            {
                new UriWithTransform{ Uri =new Uri("Images/2.png", UriKind.Relative), Height=173, Width=173, TranslateTransform=new TranslateTransform() {X=0, Y=0 }},
                new UriWithTransform{ Uri =new Uri("Images/5.png", UriKind.Relative), Height=73, Width=100, TranslateTransform=new TranslateTransform() { X=73,Y=0}},
                new UriWithTransform{ Uri =new Uri("Images/6.png", UriKind.Relative), Height=33, Width=33, TranslateTransform=new TranslateTransform() { X=140,Y=0}}
            };
            var tileUrl = TileImageGenerator.GenerateTile(uris, "UrisLiveTileImage");
            var tile = ShellTile.ActiveTiles.FirstOrDefault(t => t.NavigationUri == new Uri("/UtilitesSamples/TileGeneratorSample.xaml", UriKind.Relative));
            if (tile != null)
                tile.Delete();
            ShellTile.Create(new Uri("/UtilitesSamples/TileGeneratorSample.xaml", UriKind.Relative), new StandardTileData { Title = "Multilayer", BackgroundImage = tileUrl });
        }

        private void CreateGraphTile(object sender, MouseButtonEventArgs e)
        {
            var data = new List<List<double>>
            {
                new List<double>{ 1, -20, 25, 20, 13, 0, 50 },
                new List<double>{ 10, 20, 25, 20, 0, 18, 50 }
            };

            var legends = new List<string> { "USD", "EUR" };

            var tileUrl = TileImageGenerator.GenerateGraphOnTile(data, legends, "UrisLiveTileImage");
            var tile = ShellTile.ActiveTiles.FirstOrDefault(t => t.NavigationUri == new Uri("/UtilitesSamples/TileGeneratorSample.xaml", UriKind.Relative));
            if (tile != null)
                tile.Delete();
            if(!Mangopollo.Utils.CanUseLiveTiles) {
            ShellTile.Create(new Uri("/UtilitesSamples/TileGeneratorSample.xaml", UriKind.Relative), new StandardTileData { Title = "Graph", BackgroundImage = tileUrl });
            }
            else
            {
                var smallUrl = TileImageGenerator.GenerateGraphOnTile(data, legends, "UrisLiveTileImageSmall", TileSize.Wp8SmallTileSize);
                var wideUrl = TileImageGenerator.GenerateGraphOnTile(data, legends, "UrisLiveTileImageWide", TileSize.Wp8WideTileSize);
                var tileData = Mangopollo.Tiles.TilesCreator.CreateFlipTile("Graph", null, null, null, null, smallUrl, tileUrl, null, wideUrl, null);
                Mangopollo.Tiles.ShellTileExt.Create(new Uri("/UtilitesSamples/TileGeneratorSample.xaml", UriKind.Relative), tileData, true);
            }
        }
    }
}